package com.yuneec.fly.dialogs;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.TextView;

import com.yuneec.fly.R;

/**
 * Created by zd.zhang on 2016/6/22.
 */
public class ProgressDialog extends DialogFragment {

    private TextView msg;
    private View mView;
    private String message;
    private DialogInterface.OnCancelListener onCancelListener;

    public ProgressDialog() {
        super();
        setCancelable(false);
    }

    public void setMessage(String message) {
        this.message = message;
        if (msg != null) {
            msg.setText(message);
        }
    }

    public void setOnCancelListener(DialogInterface.OnCancelListener onCancelListener) {
        this.onCancelListener = onCancelListener;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        if (mView == null) {
            mView = inflater.inflate(R.layout.progress_dialog, container, container != null);
            msg = (TextView) mView.findViewById(R.id.msg);
        } else {
            ViewParent parent = mView.getParent();
            if (parent != null && parent instanceof ViewGroup) {
                ((ViewGroup) parent).removeView(mView);
            }
        }
        if (TextUtils.isEmpty(message)) {
            String tag = getTag();
            if (tag != null) {
                msg.setText(getTag());
            }
        } else {
            msg.setText(message);
        }
        return mView;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = new Dialog(getActivity(), R.style.PointsSaveDialog);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setCancelable(false);
        return dialog;
    }

    @Override
    public void onCancel(DialogInterface dialog) {
        super.onCancel(dialog);
        if (onCancelListener != null) {
            onCancelListener.onCancel(dialog);
        }
    }
}
